package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoService roomInfoService;

    @Override
    public void saveOrUpdateApartmentInfo(ApartmentSubmitVo apartmentSubmitVo) {
        //获取公寓id
        Long id = apartmentSubmitVo.getId();
        //调用保存或更新的方法保存或更新公寓信息
        this.saveOrUpdate(apartmentSubmitVo);
        //判断od是否为null
        if (id != null){
            //证明在更新公寓信息,删除中间表的数据
            //1.根据id删除公寓的配套信息
            LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
            apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
            //2.根据id删除公寓的标签信息
            LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
            apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
            //3.根据id删除公寓的杂费信息
            LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
            apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
            //4.根据id删除公寓的图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);

        }
        //1.保存公寓配套
        //获取所有公寓配套id
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        //判断是否为空
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            //创建一个集合
            List<ApartmentFacility> apartmentFacilityList = new ArrayList<>();
            //遍历所有公寓配套id
            for (Long facilityInfoId : facilityInfoIds) {
                //创建ApartmentFacility对象
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .apartmentId(apartmentSubmitVo.getId())
                        .facilityId(facilityInfoId).build();
                apartmentFacilityList.add(apartmentFacility);
            }
            //批量插入
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }

        //2.保存公寓标签信息
        //获取所有公寓标签id
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        //判断是否为空
        if (!CollectionUtils.isEmpty(labelIds)){
            //创建一个集合
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            //遍历
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder().labelId(labelId).apartmentId(apartmentSubmitVo.getId()).build();
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }
        //3.保存公寓杂费信息
        //获取所有公寓杂费id
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        //判断是否为空
        if (!CollectionUtils.isEmpty(feeValueIds)){
            //创建一个集合
            List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            //遍历
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder().feeValueId(feeValueId).apartmentId(apartmentSubmitVo.getId()).build();
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }

        //4.获取所有公寓图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        //判断是否为空
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

    }

    @Override
    public void deleteById(Long id) {
        //删除之前判断是否有房间
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId, id);
        long count = roomInfoService.count(lambdaQueryWrapper);
        if (count>0){
            throw new  LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //删除公寓信息
        this.removeById(id);
        //删除中间表信息
        //1.根据id删除公寓的配套信息
        LambdaQueryWrapper<ApartmentFacility> apartmentFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFacilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(apartmentFacilityLambdaQueryWrapper);
        //2.根据id删除公寓的标签信息
        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(apartmentLabelLambdaQueryWrapper);
        //3.根据id删除公寓的杂费信息
        LambdaQueryWrapper<ApartmentFeeValue> apartmentFeeValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentFeeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(apartmentFeeValueLambdaQueryWrapper);
        //4.根据id删除公寓的图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.APARTMENT).eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
    }

    @Override
    public IPage<ApartmentItemVo> getPageList(Page<Object> page, ApartmentQueryVo queryVo) {
        return baseMapper.getPageList(page,queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        //获取apartmentInfo信息
        ApartmentInfo apartmentInfo = this.getById(id);
        //1. 根据公寓id和item_type查询公寓图片
        List<GraphVo> graphVoList = graphInfoMapper.getApartmentPicsByIdAndItemType(id,ItemType.APARTMENT);
        //2.根据公寓id查询标签列表
        List<LabelInfo> labelInfoList = labelInfoMapper.getApartmentLabelInfoById(id);
        //3.根据公寓id查询配套列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getApartmentFacilityInfoById(id);
        //4.根据公寓id查询杂费列表
        List<FeeValueVo> feeValueList = feeValueMapper.getApartmentFeeValueById(id);
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //为apartmentDetailVo的apartmentInfo中的属性赋值
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        //设置公寓的图片
        apartmentDetailVo.setGraphVoList(graphVoList);
        //设置公寓标签
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        //设置公寓配套
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        //设置公寓杂费
        apartmentDetailVo.setFeeValueVoList(feeValueList);
        return apartmentDetailVo;
    }
}




